<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :before-close="closeDialog" v-on:open="loadData">
      <el-form :model="form" :rules="rules"ref="form" label-width="200px" class="demo-form">
        <el-form-item label="环节类型名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="是否需要确认受试者身份" prop="checkSubjects">
          <el-radio class="radio" v-model="form.checkSubjects" :label="1">是</el-radio>
          <el-radio class="radio" v-model="form.checkSubjects" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="是否涉及容器" prop="hasContainer">
          <el-radio class="radio" v-model="form.hasContainer" :label="1">是</el-radio>
          <el-radio class="radio" v-model="form.hasContainer" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="容器名称" prop="containerName" v-if="form.hasContainer">
          <el-input v-model="form.containerName"></el-input>
        </el-form-item>
        <el-form-item label="父级环节类型" prop="containerName">
          <el-select v-model="form.parentId">
            <el-option v-for="item in $parent.list" v-if="item.lvl==1" :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sortNum">
          <el-input v-model="form.sortNum" type="number"></el-input>
        </el-form-item>
        <el-form-item label="环节类型说明">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="24" style="padding:10px;">
            <span style="color:red;float:left;">"*"为必填项。</span>
            <div class="pull-right">
          <el-button v-if="$utils.ckUIPower('S01010101')" type="primary" @click.native.prevent="handleSubmit" :disabled="editClick">{{addBtnTxt}}</el-button>
          <el-button v-if="$utils.ckUIPower('S01010101')" @click.native.prevent="handleReset">重置</el-button>
          <el-button @click.native.prevent="closeDialog">取消</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import http from '../../../api/http'
  var model = {
    hasContainer: '',
    name: '',
    remark: '',
    id: '',
    parentId: '',
    containerName: '',
    checkSubjects: '',
    lvl: 1,
    projectId: ''
  };
  export default {
    data () {
      return {
        editClick:false,
        title: '测试环节添加', form: Object.assign({}, model),
        rules: {
          name: [
            {required: true, message: '请输入环节类型名称'},
            {min: 1, max: 20, message: '长度在 1 到 20 个字符'}
          ],
          checkSubjects:[
            {required: true, message: '请选择是否需要确认受试者身份'}
          ],
          hasContainer:[
            {required: true, message: '请选择是否需要涉及容器'}
          ]
        }, roleList: [], addBtnTxt: '',
        siteList: []
      }
    },
    props: {
      dialogFormVisible: {
        type: Boolean,
        default: false
      },
      id:{
          type:String
      }
    },
    methods: {
      handleReset () {
          this.form.parentId = null;
        this.$refs.form.resetFields()
      },
      handleSubmit (ev) {
        var vm = this;
        this.$refs.form.validate(function (valid) {
          if (valid) {
            vm.editClick = true;
            vm.form.projectId = vm.$utils.getCurrenrProject().id;
            http.postData(vm, "/pt/linkType/save", {json: JSON.stringify(vm.form)}, function (resp) {
              var ret = resp.body;
              if (ret.code === '111') {
                vm.form = Object.assign({}, model);
                vm.closeDialog();
                vm.$parent.onSubmit();
                vm.$notify({title: '操作提示', message: ret.msg, type: 'success'});
              } else {
                vm.editClick = true;
                vm.$notify.error({title: '操作失败', message: ret.msg});
              }
            });
          } else {
            return false
          }
        })
      }, closeDialog(){
        this.editClick = false;
        this.$emit('closeDialog');
        this.form=Object.assign({}, model);
      }, loadData(){
        var vm = this;
        if (this.id != undefined && this.id != '') {
          http.postData(vm, "/pt/linkType/detail/" + this.id, {}, function (resp) {
            var ret = resp.body;
            if (ret.code === '111') {
              vm.form = ret.value;
            } else {
              vm.$notify.error({title: '操作失败', message: ret.msg});
            }
          });
        }
          if (vm.id == undefined || vm.id == '') {
            vm.addBtnTxt = '立即创建';
            vm.title = '试验环节类型创建';
          } else {
            vm.addBtnTxt = '保存修改';
            vm.title = "试验环节修改"
          }
      }
    }, created() {
    }
  }
</script>
